package com.ymdx.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 用来实现部分路径直接跳转，这里主要因为主页的index.html路径，
 * 无法通过web.xml里面的welcome-frst-list配置，所以手动把/转发给index.html页面
 * Created by yangliwei on 2017/10/29.
 */
public class HtmlFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String path = req.getServletPath();  //此方法只有HttpServletRequest类中有，获得页面响应的路径
        if ("/".equals(path)) {
            request.getRequestDispatcher("/pages/index.html").forward(request, response);
        } else if (path.contains("*.html")) {
            System.out.println(path);
            request.getRequestDispatcher("/pages/index.html").forward(request, response);
        } else
            filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
